﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ProjectHT.YKDGVB2012.Core.Datacontract;
using Telerik.Web.UI;

namespace ProjectHT.YKDGVB2012.Core.Webpage
{
    public class DuThaoVanban : LayoutsPageBase
    {
        private IMainList _imainlist;
        protected override bool AllowAnonymousAccess
        {
            get { return false; }
        }


        protected RadGrid _raddtGrid;
        protected override void OnInit(EventArgs e)
        {
            _raddtGrid.NeedDataSource += new GridNeedDataSourceEventHandler(_raddtGrid_NeedDataSource);
        }

        protected void _raddtGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            _raddtGrid.DataSource = Getdata();
        }
        public DuThaoVanban()
        {

            var services = new MainServices(SPContext.Current);
            _imainlist = services;

        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public List<Mainlist> Getdata()
        {
            var mainlist = _imainlist.GetListAll().Select(item => new Mainlist { Title = item.Title, ID = item.ID, CreatedDate = item["Created"].ToString(), Status = (bool)item[Common.MainList.FIELDSTATUS], Expires = (bool)item[Common.MainList.FIELDEXPIRE] ,Exdate = Convert.ToDateTime(item[Common.MainList.FIELDEXPIREDATE])}).ToList();
            return mainlist.OrderByDescending(p => p.CreatedDate).ToList();
        }

        protected string GetAttach(string id)
        {
            var str = string.Empty;
            if (String.IsNullOrEmpty(id)) return string.Empty;
            var item = _imainlist.GetbyId(Convert.ToInt32(id));
            var acttach = item.Attachments.Count;
            if (acttach > 0)
            {

                for (int i = 0; i < acttach; i++)
                {
                    var filepath =  "/lists/" + Common.MainList.MAINLIST + "/Attachments/" + item.ID + "/" +
                                   item.Attachments[i];
                    str = "<a href='" + filepath + "'>" + item.Attachments[i] + "</a>";
                }
            }
            return str;
        }
    }
}
